package MultiThread.MultiThreadPrint;

/*
用syn的最简单的方法
 */
public class MultiThreadPrint_1 {
    private static Integer num = 1;

    static class Thread1 implements Runnable {
        @Override
        public void run() {
            while (num <= 10) {
                synchronized (this) {
                    if (num % 3 == 1) {
                        System.out.println("Thread1: " + num);
                        num++;
                    }
                }
            }
        }
    }

    static class Thread2 implements Runnable {
        @Override
        public void run() {
            while (num <= 10) {
                synchronized (this) {
                    if (num % 3 == 2) {
                        System.out.println("Thread2: " + num);
                        num++;
                    }
                }
            }
        }
    }

    static class Thread3 implements Runnable {
        @Override
        public void run() {
            while (num <= 10) {
                synchronized (this) {
                    if (num % 3 == 0) {
                        System.out.println("Thread3: " + num);
                        num++;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Thread1());
        Thread thread2 = new Thread(new Thread2());
        Thread thread3 = new Thread(new Thread3());
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
